import { useEffect, useRef, useState } from "react";
import HomePage_TableComponent from "../../../../component/table"
import HomePage_ModalComponent from "../../../../component/modal";
import HomePage_DetailModalComponent from "../../../../component/detailModal";
import dayjs from 'dayjs';
import { $Http } from "../../../../../../network";
import { message } from "antd";
import { $eventBus, $EventBus_off, $EventBus_on, $export_excel } from "../../../../../../common/utils";
// 主页-统计查询页面-事件统计页面
const HomePage_StatisticalQueryPage_EventStatisticsPage: React.FC = () => {
  const initParamsData = useRef<object>({})
  const paramsData = useRef<object>({})
  let [pageNo, setPageNo] = useState<number>(1)
  let [pageSize, setPageSize] = useState<number>(10)
  const [sourceData, setSourceData] = useState<object>({})
  const [isModal, setIsModal] = useState<boolean>(false)
  const [modalData, setModalData] = useState<object>({})
  const columnList = [
    {
      title: '序号',
      width: 60,
      render: (text: string, record: object, idx: number) => idx + 1,
    },
    {
      title: '发生时间',
      minWidth: 85,
      dataIndex: 'startTime',
      render: (text: string) => text ?? "-"
    },
    {
      title: '确认时间',
      minWidth: 85,
      dataIndex: 'confirmTime',
      render: (text: string) => text ?? "-"
    },

    {
      title: '站点',
      minWidth: 60,
      dataIndex: 'stationName',
      render: (text: string) => text ?? "-"
    },
    {
      title: '事件对象',
      minWidth: 85,
      dataIndex: 'warningName',
      render: (text: string) => text ?? "-"
    },
    {
      title: '数据类型',
      minWidth: 85,
      dataIndex: 'dataType',
      align: "center",
      render: (text: string) => text ?? "-"
    },
    {
      title: '越限比例',
      minWidth: 85,
      dataIndex: 'ratio',
      align: "right",
      render: (text: string) => text ?? "-"
    },
    {
      title: '预警等级',
      minWidth: 85,
      dataIndex: 'warningLevel',
      align: "center",
      render: (text: string) => text ?? "-"
    },
    {
      title: '操作员',
      minWidth: 70,
      dataIndex: 'confirmPeople',
      render: (text: string) => text ?? "-"
    },
    {
      title: '操作',
      align: "right",
      width: 60,
      fixed: "right",
      render: (text: string, record: object, idx: number) => <span className="homePage-tableOperationBtn" onClick={() => click_see(record)}>查看</span>
    },

  ];
  const http_search = () => {
    $Http("search", { typeName: "统计查询/事件统计", ...paramsData.current, pageNo, pageSize }).then((res) => {
      setSourceData({ ...res["data"] })
    })
  }

  const change_pagination = (no: number, size: number) => {
    pageNo = no
    pageSize = size
    setPageNo(pageNo)
    setPageSize(pageSize)
    http_search()
  }

  const change_export = (list: Array<number>) => {
    $export_excel("统计查询/事件统计", list)
  }

  // 广播函数数据
  const eventBusFuncData = {
    // 头部操作新增
    headerOperationAdd: (data: object) => {
      setModalData({ type: "add" })
      setIsModal(true)
    },
    // 头部操作初始参数
    headerOperationInitParamsData: (data: object) => {


      initParamsData.current = data["data"]["initParamsData"]
      paramsData.current = data["data"]["initParamsData"]
      http_search()
    },
    // 头部操作查询
    headerOperationSearch: (data: object) => {
      pageNo = 1
      setPageNo(pageNo)
      paramsData.current = { ...paramsData.current, ...data["data"]["paramsData"] }
      http_search()
    },
    // 头部操作条件的开关
    headerOperationConditionSwitch: (data: object) => {
      pageNo = 1
      setPageNo(pageNo)
      paramsData.current = { ...initParamsData.current }
      http_search()
    },
  }



  useEffect(() => {
    $EventBus_on(eventBusFuncData)
    return () => {
      $EventBus_off(eventBusFuncData)
    }
  }, [])
  const click_see = (data: object) => {
    setModalData(data)
    setIsModal(true)
  }

  return (
    <>
      <HomePage_TableComponent name="homePageStatisticalQueryPageEventStatisticsPageTable" isRowSelection alignCenterList={["数据类型", "预警等级"]} alignRightList={["越限比例", "操作"]} columnList={columnList} pageNo={pageNo} pageSize={pageSize} sourceData={sourceData} change_pagination={change_pagination} change_selectedRowList={change_export} />
      {
        isModal && <HomePage_ModalComponent width={1000} change_cancel={setIsModal} contentComponent={<HomePage_DetailModalComponent name="预警" modalData={modalData} change_cancel={() => setIsModal(false)} />} />
      }
    </>
  )
}

export default HomePage_StatisticalQueryPage_EventStatisticsPage